﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Besture.Bll;
using Besture.Common;
using Besture.Common.Definition;
using Besture.UI.ClassHelper;

using DevExpress.Utils;
using DevExpress.Web.ASPxGridView;

namespace Besture.UI.Admin.LabelManage
{
    public partial class ExecuteSql : BasePage
    {
        //文本框ID
        public string _ID = StringHelper.Query("id");

        /// <summary>
        /// 初始化页面
        /// </summary>
        protected override void InitialLoad()
        {
            BindData();
            InitAspxGridView(grid);
        }

        /// <summary>
        /// 初始化GridView控件
        /// </summary>
        public static void InitAspxGridView(ASPxGridView gridView)
        {
            gridView.SettingsLoadingPanel.Text = "正在加载中...";
            gridView.Images.SpriteCssFilePath = "/Admin/Themes/DevTheme/DevExOABlue/{0}/sprite.css";
            gridView.Images.LoadingPanelOnStatusBar.Url = "/Admin/Themes/DevTheme/DevExOABlue/GridView/gvLoadingOnStatusBar.gif";
            gridView.Images.LoadingPanel.Url = "/Admin/Themes/DevTheme/DevExOABlue/GridView/Loading.gif";
            gridView.Styles.CssFilePath = "/Admin/Themes/DevTheme/DevExOABlue/{0}/styles.css";
            gridView.Styles.CssPostfix = "DevExOABlue";
            gridView.Styles.Header.ImageSpacing = 5;
            gridView.Styles.Header.SortingImageSpacing = 5;
            gridView.StylesEditors.CalendarHeader.Spacing = 1;
            gridView.StylesEditors.ProgressBar.Height = 25;
            gridView.Settings.ShowFooter = false;
            gridView.Settings.ShowFilterRow = false;
            //隔行换色
            gridView.Styles.AlternatingRow.Enabled = DefaultBoolean.True;
            //删除提示
            gridView.SettingsBehavior.ColumnResizeMode = ColumnResizeMode.NextColumn;
            gridView.Settings.ShowHeaderFilterButton = false;
            gridView.Settings.EnableFilterControlPopupMenuScrolling = false;

            //不显示加载图片
            gridView.SettingsLoadingPanel.ShowImage = true;
        }

        /// <summary>
        /// 回发
        /// </summary>
        protected override void PostBackLoad()
        {
            BindData();
        }

       protected void BindData()
       {
           if(string.IsNullOrEmpty(txtSql.Text))
           {
               return;
           }
           try
           {
               DataTable table = QueryDac.GetTable(txtSql.Text);
               grid.Columns.Clear();
               for (int i = 0; i < table.Columns.Count; i++)
               {
                   GridViewDataTextColumn gridViewDataTextColumn = new GridViewDataTextColumn();
                   gridViewDataTextColumn.FieldName = table.Columns[i].ColumnName;
                   grid.Columns.Add(gridViewDataTextColumn);
               }
               grid.DataSource = table;
               grid.DataBind();
           }
           catch (Exception exception)
           {
               string tips = string.Format(@"提示：Sql查询出现错误！【Exception：{0}】", exception.Message.ToString());
               lblError.Text = tips;
               PageAlert(tips);
           }
       }

        protected void Btn_Click(object sender,EventArgs e)
        {
            BindData();
        }
    }
}